


local message = require 'jass.message'
local console = ac.console
local buttons = {}
for i,panel in ipairs(console.itemdetail) do
    local button = panel:add_button('')
    button.slotid = panel.slotid
    button.exiocn = panel:add_panel()
    button._effect = button:child_builder {
        parent = button,
        type = 'model',
        model = [[UI\Feedback\Autocast\UI-ModalButtonOn.mdl]],
        x = 4,
        y = 2,
        scales = {button.w / 94,button.h / 70},
        show_flag = false,
    }

    panel.effect_button = button

    button.on_button_clicked = function(self)
        local ability, order = message.button(3, 2)
        if order ~= 0xD000B then
            local unit = ac.unit.localunit()
            local item = unit.items and unit.items[self.slotid]
            if unit and item then
                ac.console:notify("控制台-点击物品按钮", self.slotid)
                ac.sync('同步-点击物品按钮',unit.handle,item.handle)
            end
        end
    end 
    button.on_button_right_mousedown = function(self)
        ac.console:notify("控制台-右击物品按钮", self.slotid)
    end 
    
    table.insert(buttons,button)
end

ac.sync:event '同步-点击物品按钮'(function(_,player,handle,it_handle)
    local unit = ac.unit(handle)
    local item = ac.item.j_item(it_handle)
    if unit and item and unit.owner==player and item.owner==unit then
        unit:notify('单位-点击物品',unit,item)
    end
end)

ac.hardware:event '界面绘制'(function()
    local unit = ac.unit:localunit()
    for _,button in ipairs(buttons) do
        local item = unit and unit.items[button.slotid]
        if item and item.show_effect then
            button._effect:show()
        else
            button._effect:hide()
        end
        if item and item.exiocn then
            button.exiocn:set_normal_image(item.exiocn)
        else
            button.exiocn:set_normal_image('')
        end
    end
end)

return panel
